package org.prosim.user.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.json.annotations.JSON;
import org.prosim.action.BaseAction;
import org.prosim.persistence.HibernateWebUtils;
import org.prosim.persistence.Page;
import org.prosim.persistence.PropertyFilter;
import org.prosim.user.entity.Role;
import org.prosim.user.service.UserService;
import org.prosim.util.Struts2Utils;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("serial")
@ParentPackage("json-default") 
@Namespace("/user")
@InterceptorRefs({ @InterceptorRef("paramsPrepareParamsStack") })
@Results({ @Result(name = BaseAction.RELOAD, location = "role-list", type="chain") })
public class RoleAction extends BaseAction {
	
	private Integer roleId;
	private Role role;
	private List<Role> roleList;
	private List<String[]> roleData = new ArrayList<String[]>();
	private Page<Role> page = new Page<Role>(5);
	private Integer workingVersion;
	private List<Integer> roleIds = new ArrayList<Integer>();
	private String batchType;

	@Autowired
	private UserService userService;

	
	@Action("role-list")
	public String rolelist() throws Exception {
		List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		page = userService.searchUser(page, filters);
		return SUCCESS;		
	}
	
	
	@Action(
		value = "role-data", 
		results = @Result(type="json", name=SUCCESS)
	)	
	public String roleData() throws Exception {
		roleList = userService.getAllRole();
		for (Role role : roleList) {
			String[] data = new String[3];
			data[0] = role.getId() + "";
			data[1] = role.getCode();
			data[2] = role.getName();
			roleData.add(data);
		}
		
		return SUCCESS;	
	}
	
	
	@Action("role-add-form")
	public String roleAddForm() {
		return SUCCESS;
	}
	
	
	@Action("role-add")
	public String roleAdd() {
		userService.saveRole(role);
		addSuccessMessage(getText("user.role.add.success"));		
		return RELOAD;
	}
	
	
	@Action("role-edit-form")
	public String roleEditForm() throws IllegalAccessException, InvocationTargetException {
		role = userService.getRole(roleId);
		return SUCCESS;
	}

	
	
	@Action("role-edit")
	public String roleEdit() {
		userService.editRole(role);
		addSuccessMessage(getText("user.role.edit.success"));		
		return RELOAD;
	}


	@Action("role-delete")
	public String roleDelete() {
		userService.deleteRole(roleId);
		addSuccessMessage(getText("user.role.delete.success"));		
		return RELOAD;
	}
	
	
	@Action("role-batch")
	public String roleBatch() {
		if(batchType != null && !batchType.equals("")) {
			if(batchType.equals("delete")) {
				userService.batchDeleteRole(roleIds);
				addSuccessMessage(getText("user.role.batch.delete.success"));				
			}
		}
		return RELOAD;
	}
	
	
	@Action("role-form-validate")
	public String roleFormValidate() throws IOException {
		String type = request.getParameter("type");
		if(StringUtils.equals(type, "code")) {
			String code = request.getParameter("code");
			response.getWriter().write(String.valueOf(StringUtils.equals(code, "1122")));
		}
		return null;
	}
	
	

	/**
	 * @return the roleList
	 */
	public List<Role> getRoleList() {
		return roleList;
	}


	/**
	 * @return the role
	 */
	public Role getRole() {
		return role;
	}


	/**
	 * @param role the role to set
	 */
	public void setRole(Role role) {
		this.role = role;
	}


	/**
	 * @return the roleId
	 */
	public Integer getRoleId() {
		return roleId;
	}


	/**
	 * @param roleId the roleId to set
	 */
	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}


	/**
	 * @return the roleIds
	 */
	public List<Integer> getRoleIds() {
		return roleIds;
	}


	/**
	 * @param roleIds the roleIds to set
	 */
	public void setRoleIds(List<Integer> roleIds) {
		this.roleIds = roleIds;
	}


	/**
	 * @return the batchType
	 */
	public String getBatchType() {
		return batchType;
	}


	/**
	 * @param batchType the batchType to set
	 */
	public void setBatchType(String batchType) {
		this.batchType = batchType;
	}


	/**
	 * @return the page
	 */
	public Page<Role> getPage() {
		return page;
	}


	/**
	 * @param page the page to set
	 */
	public void setPage(Page<Role> page) {
		this.page = page;
	}


	/**
	 * @return the roleData
	 */
	@JSON(name="aaData")
	public List<String[]> getRoleData() {
		return roleData;
	}


	/**
	 * @param roleData the roleData to set
	 */
	public void setRoleData(List<String[]> roleData) {
		this.roleData = roleData;
	}
	
	
	

}
